<template>
  <div class="layout-container">
      
    <!-- 主要内容 -->
    <el-main class="main-content">
      <router-view v-slot="{ Component }">
        <transition name="fade" mode="out-in">
          <component :is="Component" />
        </transition>
      </router-view>
    </el-main>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useAuthStore } from '@/stores/auth'
import { useI18n } from 'vue-i18n'
import { useThemeStore } from '@/stores/theme'
import { Sunny, Moon } from '@element-plus/icons-vue'
import LanguageIcon from '@/components/icons/LanguageIcon.vue'

const { t, locale } = useI18n()
const themeStore = useThemeStore()

const route = useRoute()
const router = useRouter()
const authStore = useAuthStore()

// 面包屑导航
const breadcrumbs = computed(() => {
  return route.matched.filter(item => item.meta?.title)
})

// 用户头像
const userAvatar = ref('')

// 切换主题
const toggleTheme = () => {
  const currentTheme = themeStore.theme
  if (currentTheme === 'light') {
    themeStore.setTheme('dark')
  } else if (currentTheme === 'dark') {
    themeStore.setTheme('auto')
  } else {
    themeStore.setTheme('light')
  }
}

// 切换语言
const changeLanguage = (lang: string) => {
  locale.value = lang
  localStorage.setItem('language', lang)
}

// 处理下拉菜单命令
const handleCommand = async (command: string) => {
  switch (command) {
    case 'profile':
      router.push('/profile')
      break
    case 'user-role-permission':
      router.push('/user-role-permission')
      break
    case 'settings':
      ElMessage.info(t('messages.settingsDeveloping'))
      break
    case 'logout':
      try {
        await ElMessageBox.confirm(
          t('layout.logoutConfirm'),
          t('common.info'),
          {
            confirmButtonText: t('common.confirm'),
            cancelButtonText: t('common.cancel'),
            type: 'warning'
          }
        )
        
        authStore.logout()
        ElMessage.success(t('layout.logoutSuccess'))
        router.push('/login')
      } catch {
        // 用户取消操作
      }
      break
  }
}

// 初始化
onMounted(() => {
  // 检查是否已登录
  authStore.initUser()
  if (!authStore.isAuthenticated) {
    router.push('/login')
  }
})

// 监听用户权限变化，重新加载权限
watch(() => authStore.user, () => {
  if (authStore.isAuthenticated) {
    authStore.loadUserPermissions()
  }
}, { immediate: true })
</script>

<style scoped>
.layout-container {
  height: 100vh;
  background-color: var(--bg-color-secondary);
  display: flex;
  flex-direction: column;
}

.header {
  background-color: var(--header-bg);
  border-bottom: 1px solid var(--border-color-light);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  height: 64px;
  flex-shrink: 0;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.logo-text {
  color: var(--text-color);
  font-size: 18px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  padding: 6px 12px;
  border-radius: var(--radius-md);
  transition: all 0.2s ease;
  background-color: transparent;
}

.user-info:hover {
  background-color: var(--card-bg-hover);
}

.username {
  font-size: 15px;
  color: var(--text-color);
  font-weight: 500;
}

.theme-switcher {
  padding: 0;
  height: 40px;
  width: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-md);
  color: var(--text-color-secondary);
  transition: all 0.2s ease;
}

.theme-switcher:hover {
  background-color: var(--card-bg-hover);
  color: var(--text-color);
}

.language-switcher .el-dropdown-menu {
  min-width: 120px;
}

.language-switcher .flag {
  margin-right: 8px;
}

.language-switcher .is-active {
  color: var(--el-color-primary);
  font-weight: 600;
}

.main-content {
  background-color: var(--bg-color-secondary);
  padding: 24px;
  overflow-y: auto;
  flex: 1;
}

/* 过渡动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header {
    padding: 0 16px;
  }
  
  .username {
    display: none;
  }
  
  .main-content {
    padding: 16px;
  }
  
  .logo-text {
    font-size: 16px;
  }
}
</style>
